package com.xuanwu.mall.util.common;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * xuanwu-mall
 * com.xuanwu.mall.util.common
 *
 * @Description:通用响应对象
 * @Author：xuanwudeshuijngye_26567
 * @Date 2024-03-14 16:30
 * @Company: 旋舞科技有限公司
 **/
@Data
@Accessors(chain = true)
@NoArgsConstructor
public class RespResult <T> implements Serializable {
    /**
     * 响应码
     */
    private String code;
    /**
     * 响应信息
     */
    private String msg;
    /**
     * 响应体
     */
    private T data;

    /**
     * 默认成功的返回
     */
    public static <T> RespResult<T> defaultSuccessResult() {
        return buildResult(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(),null);
    }
    /**
     *成功的返回,data为返回的数据
     */
    public static <T> RespResult<T> successResult(T data) {
        return  buildResult(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), data);
    }
    public static <T> RespResult<T> successResult(String msg,T data) {
        return  buildResult(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    /**
     *默认失败的返回
     */
    public static <T> RespResult<T> defaultFailResult() {
        return  buildResult(ResponseCode.FAIL.getCode(), ResponseCode.FAIL.getMsg(),null);
    }
    public static <T> RespResult<T> failResult(String msg) {
        return  buildResult(ResponseCode.FAIL.getCode(), msg,null);
    }

    public static <T> RespResult<T> failResult(T data) {
        return  buildResult(ResponseCode.FAIL.getCode(), ResponseCode.FAIL.getMsg(),data);
    }

    public static <T> RespResult<T> failResult(String msg,T data) {
        return  buildResult(ResponseCode.FAIL.getCode(), msg,data);
    }
    private static <T> RespResult<T> buildResult(String code, String msg, T data) {
        RespResult<T> result = new RespResult<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
    /*****结果集转化为Map************************************************/
    public Map<String ,Object> buildResultMap(String code, String msg, T data){
        HashMap<String ,Object> resultMap=new HashMap<>();
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        resultMap.put("data",data);
        return resultMap;
    }
}

